package teamdamn.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;

import teamdamn.controller.DBIO;
import teamdamn.model.ResponseList;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;

/**
 * This is the Main Gui Frame and serves as the Runner of the Program
 * @author kjudoy
 * @edited joelhun
 */
@SuppressWarnings("serial")
public class GUI extends JFrame {

	private final String preUpdateFilePath;
	
	private JPanel contentPane;
	
	private JPanel currentPanel;
	
	private LoginPanel myLoginPanel; 	
	private SearchPanel mySearchPanel;	
	private RFPPanel myRFPPanel;	
	private RefreshPanel myRefreshPanel;
	private AnalystPanel myAnalystPanel;
	
	private ResponseList myResponseList;
	
	private JLabel lblHelloUser;
	
	private JButton btnLogout;

	/**
	 * Constructor of GUI Frame
	 * @author kjudoy
	 * @edited joelhun
	 */
	public GUI() {	
	    File myPath = new File("repository");
        preUpdateFilePath = myPath.getAbsolutePath() + DBIO.PathSeparator() + "PreUpdateQuestions.txt";
		mySearchPanel = new SearchPanel();
		myResponseList = new ResponseList();
		myRefreshPanel = new RefreshPanel(this, myResponseList);
		myResponseList.registerObserver(mySearchPanel);
		myResponseList.updateResponses(preUpdateFilePath);
		myLoginPanel = new LoginPanel(this);
		myAnalystPanel = new AnalystPanel(mySearchPanel);

		buildGUI();		
	}
	
	/**
	 * Used when the logout button is pressed, swaps any panel to Login Panel
	 * @author jrsto674
	 */
	public void toLogin() {
		currentPanel.remove(currentPanel.getComponent(0));
		myLoginPanel.resetFields();
		currentPanel.add(myLoginPanel);
		updateUsername("New User");
		currentPanel.revalidate();
		currentPanel.repaint();
		btnLogout.setVisible(false);
	}
	
//	/**
//	 * This method for swaps the Login Panel to RFP Panel
//	 * @author kjudoy
//	 */
//	public void loginToRFP() {
//		currentPanel.remove(currentPanel.getComponent(0));
//		currentPanel.add(myAnalystPanel);
//		currentPanel.revalidate();
//		btnLogout.setVisible(true); // change its visibility
//	}
	
	/**
	 * This method for swaps the Login Panel to RFP Panel
	 * @author kjudoy
	 * @edited joelhun
	 */
	public void loginToAnalystPanel() {
	
		if (currentPanel.getComponentCount() != 0) // checks first if a panel exist
			currentPanel.remove(currentPanel.getComponent(0));
		currentPanel.add(myAnalystPanel);
		currentPanel.revalidate();
		currentPanel.repaint();
		btnLogout.setVisible(true); // change its visibility
	}
	
	/**
	 * This method changes from the Login Panel to the Admin
	 * Data Refresh panel.
	 * @author jrsto674
	 * @edited joelhun
	 */
	public void loginToRefresh() {			
		if (currentPanel.getComponentCount() != 0) // checks first if a panel exist
			currentPanel.remove(currentPanel.getComponent(0)); 
		currentPanel.add(myRefreshPanel);
		currentPanel.revalidate();
		btnLogout.setVisible(true); // change its visibility
	}
	
	/**
	 * This method updates the welcome message in the
	 * upper right of the GUI
	 * @author jrsto674
	 */
	public void updateUsername(String username) {
		lblHelloUser.setText("Hello, " + username + "!");
		lblHelloUser.revalidate(); // This works
	}
	
	/**
	 * This method build the GUI of this frame
	 * @author kjudoy
	 * @edited joelhun
	 */
	private void buildGUI() {

		JPanel panelLogo = new JPanel();
		panelLogo.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
		panelLogo.setBackground(UIManager.getColor("Button.background"));
		ImageIcon img = new ImageIcon("resources/globe.gif");
		this.setIconImage(img.getImage());
		BufferedImage myLogoImage;
		try {
			File myPath = new File("resources");
			String myLogoString = myPath.getAbsolutePath() + DBIO.PathSeparator() + "globe.gif";
			//String myLogoString = "resources/globe.gif";
			myLogoImage = ImageIO.read(new File(myLogoString));
			panelLogo.setLayout(new BorderLayout(0, 0));
			JLabel myLogo = new JLabel(new ImageIcon(myLogoImage));
			myLogo.setBackground(UIManager.getColor("Button.background"));
			myLogo.setPreferredSize(new Dimension(72,72));
			panelLogo.add(myLogo);		
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		JPanel panelHead = new JPanel();
		panelHead.setBorder(new LineBorder(new Color(0, 0, 0)));
		
		currentPanel = new JPanel();
		currentPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		
		JPanel panelUserInfo = new JPanel();
		panelUserInfo.setBorder(new LineBorder(new Color(0, 0, 0)));
		currentPanel.setLayout(new BorderLayout(0, 0));
				
		currentPanel.add(myLoginPanel); // Initial Login Panel

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 721, 501);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		lblHelloUser = new JLabel("Hello, New User!");

		JLabel lblNewLabel = new JLabel("RFP On Track");
		
		
		// THIS THE GROUP LAYOUT FOR THE GUIS
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(panelLogo, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panelHead, GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panelUserInfo, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)
					.addGap(2))
				.addComponent(currentPanel, GroupLayout.DEFAULT_SIZE, 695, Short.MAX_VALUE)
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(panelUserInfo, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE)
						.addComponent(panelHead, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE)
						.addComponent(panelLogo, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE))
					.addGap(8)
					.addComponent(currentPanel, GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE))
		);
		
		btnLogout = new JButton("Logout");
		btnLogout.setVisible(false);
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				toLogin();
			}
		});
		
		GroupLayout gl_panelUserInfo = new GroupLayout(panelUserInfo);
		gl_panelUserInfo.setHorizontalGroup(
			gl_panelUserInfo.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panelUserInfo.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panelUserInfo.createParallelGroup(Alignment.TRAILING)
						.addComponent(lblHelloUser, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
						.addComponent(btnLogout, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_panelUserInfo.setVerticalGroup(
			gl_panelUserInfo.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panelUserInfo.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblHelloUser, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addComponent(btnLogout)
					.addContainerGap())
		);
		panelUserInfo.setLayout(gl_panelUserInfo);
		
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setFont(new Font("Arial", Font.BOLD, 40));
		GroupLayout gl_panelHead = new GroupLayout(panelHead);
		gl_panelHead.setHorizontalGroup(
			gl_panelHead.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelHead.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_panelHead.setVerticalGroup(
			gl_panelHead.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, gl_panelHead.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE)
					.addContainerGap())
		);
		panelHead.setLayout(gl_panelHead);
		
		contentPane.setLayout(gl_contentPane);
		
	}
	

	/**
	 * Launch the application.
	 * @author kjudoy
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Throwable e) {
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GUI frame = new GUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
